package here.controller;

import here.model.Here;
import here.model.Usuario;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CadastroUsuario extends AbstractCommand{
	
	public CadastroUsuario(){
		
	}
	
	public void execute(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		try{
			Here here = Here.getInstance();
			
			String nome = request.getParameter("nome");			
			String senha = request.getParameter("senha");			
			String email = request.getParameter("email");
			
			Usuario u = new Usuario();
			u.setNome(nome);
			u.setSenha(senha);
			u.setEmail(email);
			
			here.addUsuario(u);
			
			
			List<Usuario> us = here.getUsuarios();
			for(Usuario ulist: us){
				System.out.println("Usuarios: " + ulist.getNome());
			}
			
			RequestDispatcher view = request.getRequestDispatcher("/index.jsp");
			view.forward(request, response);
			
		}catch (Exception e){
			request.setAttribute("msgErro", e.getMessage());			
			request.getRequestDispatcher("/error2.jsp").forward(request, response);	
		}
	}
}
